package com.lagou.goods.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lagou.common.pojo.Goods;
import com.lagou.goods.mapper.GoodsMapper;
import com.lagou.goods.vo.QueryCondition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.persistence.criteria.CriteriaBuilder;
import java.util.List;

/**
 * @author cyt
 * @date 2020/11/24
 */

@RestController
@RequestMapping("goods")
public class GoodsController {

    @Autowired
    private GoodsMapper goodsMapper;

    @GetMapping("/query/{id}")
    public Goods queryById(@PathVariable("id") Integer id){
        System.out.println("9201");

        Goods goods = goodsMapper.selectById(id);
        return goods;
    }

    @DeleteMapping("/delete/{id}")
    public void deleteById(@PathVariable Integer id){
        int i = goodsMapper.deleteById(id);
    }

    @PostMapping("/update")
    public void update(@RequestBody Goods goods){
        goodsMapper.updateById(goods);
    }

    @PostMapping("/query/page/{index}")
    public Page<Goods> queryByPage(@PathVariable Integer index, @RequestBody QueryCondition condition){
        Page<Goods> goods = new Page<>(index,10);
        QueryWrapper<Goods> goodsQueryWrapper = new QueryWrapper<>();
        if(condition.getName() != null){
            goodsQueryWrapper.eq("name", condition.getName());
        }else if(condition.getKind() != null){
            goodsQueryWrapper.eq("kind", condition.getKind());
        }else if(condition.getMinPrice() != null){
            goodsQueryWrapper.ge("price", condition.getMinPrice());
        }else if(condition.getMaxPrice() != null){
            goodsQueryWrapper.le("price", condition.getMaxPrice());
        }else if(condition.getMinStock() != null){
            goodsQueryWrapper.ge("stock", condition.getMinStock());
        }else if(condition.getMaxStock() != null){
            goodsQueryWrapper.le("stock", condition.getMaxStock());
        }else if(condition.getStatus() != null){
            goodsQueryWrapper.eq("status", condition.getStatus());
        }

        System.out.println(goodsQueryWrapper);
        Page<Goods> goodsPage = goodsMapper.selectPage(goods, goodsQueryWrapper);

        return goodsPage;
    }



}
